<template>
  <div class="taskPublishing">
    <div class="top_img" :style="'background-image: url(' + topBg + ')'">
      <span>帮 啦 任 务</span>
    </div>
    <div class="main">
      <div class="left">
        <div class="title">帮啦任务</div>
        <el-button @click="clikcUploadResources">添加帮啦任务</el-button>
      </div>
      <div class="right">
        <div class="tr_box">
          <div class="one_r_title">
            <span>
              帮啦任务
              <i></i>
            </span>
            <i @click="clickRefresh">刷新</i>
          </div>
          <ul>
            <li v-for="(e, i) in initData" :key="i">
              <div class="left">
                <div class="col_title">
                  <span>
                    <el-tag v-if="e.status == 0">待接单</el-tag>
                    <el-tag type="success" v-if="e.status == 1">已接单</el-tag>
                    <el-tag type="warning" v-if="e.status == 2">已解决</el-tag>
                  </span>
                  <span
                    @click="$router.push('/task_publishing/details/' + e.id)"
                  >
                    {{ e.goodsName }}
                  </span>
                </div>
                <div class="col_content">
                  <span class="name">
                    {{ e.receiverUsersName }}
                  </span>
                  <span class="time">
                    {{ $moment(parseInt(e.createdAt)).format("YYYY-MM-DD") }}
                  </span>
                  <span class="prices"> ￥{{ e.prices }} </span>
                </div>
              </div>
              <div class="right_box" @click="clickTakeOrders(e)">接受</div>
            </li>
          </ul>
        </div>
      </div>
    </div>

    <el-pagination
      style="text-align: center; margin-top: 24px"
      background
      layout="prev, pager, next"
      :total="total"
      @current-change="handleSizeChange"
      :page-size="query.pageSize"
    >
    </el-pagination>

    <el-dialog title="提示" :visible.sync="dialogVisible" width="650px">
      <div class="dialog_main">
        <div class="col_box">
          <div class="col_title">酬金：</div>
          <el-input
            v-model="queryData.prices"
            placeholder="请输入酬金"
          ></el-input>
        </div>
        <div class="col_box">
          <div class="col_title">任务标题：</div>
          <el-input
            v-model="queryData.goodsName"
            placeholder="请输入任务标题"
          ></el-input>
        </div>
        <div class="col_box">
          <div class="col_title">任务内容：</div>
          <el-input
            v-model="queryData.goodsContent"
            placeholder="请输入内容"
          ></el-input>
        </div>
        <div class="col_box">
          <div class="col_title">发布人：</div>
          <el-input
            v-model="queryData.createdBy"
            placeholder="请输入内容"
          ></el-input>
        </div>
        <div class="col_box">
          <div class="col_title">发布人手机：</div>
          <el-input
            v-model="queryData.telephone"
            placeholder="请输入内容"
          ></el-input>
        </div>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="clickCancel">取 消</el-button>
        <el-button type="primary" @click="clickSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getErrandList, getAddErrand, getErrandEdit } from "@/util/errand.js";

export default {
  data() {
    return {
      topBg:
        "http://175.178.92.128:8087/campus_help_system/images/insidePages/20190910163919740.jpg",
      initData: [],
      total: 8,
      query: {
        key: {
          goodsName: "",
          status: 0,
          checked: 1,
        },
        pageSize: 8,
        pageNum: 1,
      },
      dialogVisible: false,
      queryData: {
        goodsName: "",
        goodsContent: "",
        createdBy: "",
        telephone: "",
        prices: "",
        status: 0,
        checked: 0,
      },
    };
  },
  created() {
    this.getErrandList();
  },
  mounted() {},
  methods: {
    // 获取初始化数据
    async getErrandList() {
      let { data: res } = await getErrandList(this.query);
      this.initData = res.data.list;
      this.total = res.data.total;
    },
    // 刷新
    clickRefresh() {
      this.query.pageNum = 1;
      this.getErrandList();
      this.$message.success("刷新成功");
    },
    // 分页
    handleSizeChange() {
      this.query.pageNum = e;
      this.getErrandList();
    },

    // 点击按钮 上传资源
    clikcUploadResources() {
      if (
        window.localStorage.getItem("id") == "" ||
        window.localStorage.getItem("id") == null
      ) {
        return this.$message.error("请先登录");
      }
      this.dialogVisible = true;
    },

    // 点击按钮保存
    async clickSave() {
      // 判空
      if (this.isAddData()) {
        return;
      }
      this.queryData.checked = 0;
      this.queryData.status = 0;
      this.queryData.userId = window.localStorage.getItem("id");
      let { data: res } = await getAddErrand(this.queryData);
      console.log(res);
      if (res.status != 2000) {
        return this.$message.error(res.msg);
      }
      this.$message.success(res.msg);
      this.clickCancel();
    },
    // 关闭对话框 清空数据
    clickCancel() {
      this.queryData.goodsName = "";
      this.queryData.goodsContent = "";
      this.queryData.createdBy = "";
      this.queryData.telephone = "";
      this.queryData.prices = "";
      this.dialogVisible = false;
    },
    // 接受任务
    async clickTakeOrders(e) {
      console.log(window.localStorage.getItem("id"));
      if (
        !window.localStorage.getItem("id") ||
        window.localStorage.getItem("id") == "" ||
        window.localStorage.getItem("id") == null
      ) {
        this.$message.error("请先登录");
        return;
      }

      console.log(e);
      let query = {
        id: e.id,
        receiverUsersId: "",
        receiverUsersName: "",
        receiverUsersTelephone: "",
        receiveAt: "",
        status: 1,
      };
      query.receiverUsersId = window.localStorage.getItem("id");
      query.receiverUsersName = window.localStorage.getItem("userName");
      query.receiverUsersTelephone = window.localStorage.getItem("telephone");
      query.receiveAt = new Date();
      let { data: res } = await getErrandEdit(query);
      if (res.status != 2000) {
        return this.$message.error(res.msg);
      }
      this.$message.success(res.msg);
      this.getErrandList();
    },
    //判空
    isAddData() {
      if (this.queryData.goodsName == "" || this.queryData.goodsName == null) {
        this.$message.error("请输入任务标题");
        return true;
      }
      if (
        this.queryData.goodsContent == "" ||
        this.queryData.goodsContent == null
      ) {
        this.$message.error("请输入任务内容");
        return true;
      }
      if (this.queryData.createdBy == "" || this.queryData.createdBy == null) {
        this.$message.error("请输入名字");
        return true;
      }
      if (this.queryData.telephone == "" || this.queryData.telephone == null) {
        this.$message.error("请输入手机号码");
        return true;
      }
      if (this.queryData.prices == "" || this.queryData.prices == null) {
        this.$message.error("请输入酬金");
        return true;
      }
      return false;
    },
  },
};
</script>
<style lang="less" scoped>
.taskPublishing {
  padding-bottom: 50px;
  .top_img {
    height: 280px;
    background-repeat: no-repeat;
    background-size: auto 100%;
    background-position: center center;
    display: flex;
    align-items: center;
    justify-content: center;
    & > span {
      font-size: 48px;
      font-weight: 700;
      color: #fff;
    }
  }
  .main {
    width: 1200px;
    margin: 0 auto;
    padding-top: 50px;
    display: flex;

    .left {
      margin-right: 60px;
      .title {
        height: 50px;
        font-size: 18px;
        background: #0166b6;
        padding-left: 15px;
        width: 200px;
        line-height: 50px;
        color: #fff;
        margin-bottom: 24px;
      }
      .el-button {
        height: 46px;
        width: 200px;
      }
    }
    .right {
      flex: 1;
      .title {
        justify-content: space-between;
        display: flex;
        padding: 12px 0;
        border-bottom: 1px solid #ddd;
        & > span:nth-child(1) {
          font-size: 24px;
          color: #333333;
          line-height: 24px;
          font-size: 700;
        }
        & > span:nth-child(2) {
          & > span:nth-child(1) {
            cursor: pointer;
            margin-right: 12px;
          }
          & > span:nth-child(2) {
            color: #0e3782;
            margin-left: 12px;
          }
        }
      }
      .tr_box {
        border: 1px solid #ddd;
        padding: 18px;
        .one_r_title {
          border-bottom: 1px solid #ddd;
          margin-bottom: 15px;
          display: flex;
          justify-content: space-between;
          align-items: center;
          & > span {
            position: relative;
            display: inline-block;
            font-size: 18px;
            padding: 0 18px;
            line-height: 50px;
            height: 50px;
            i {
              position: absolute;
              width: 100%;
              height: 4px;
              background-color: #0166b6;
              left: 0;
              bottom: -2px;
            }
          }
          & > {
            color: #0166b6;
            cursor: pointer;
          }
        }
        & > ul {
          & > li {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 15px;
            padding-bottom: 15px;
            border-bottom: 1px solid #ddd;
            .left {
              .col_title {
                margin-bottom: 12px;
                display: flex;
                align-items: center;
                & > span:nth-child(1) {
                  margin-right: 12px;
                }
                & > span:nth-child(2) {
                  font-size: 20px;
                  cursor: pointer;
                }
              }
              .col_content {
                .name {
                  color: #999;
                  margin-right: 12px;
                }
                .time {
                  color: #999;
                  margin-right: 12px;
                }
                .prices {
                  color: #0166b6;
                  font-weight: 700;
                }
              }
            }
            .right_box {
              cursor: pointer;
              color: #0166b6;
              padding: 12px;
            }
          }
          & > li:last-child {
            border-bottom: none;
            margin-bottom: 0;
            padding: 0;
          }
        }
      }
    }
  }
  .dialog_main {
    .col_box {
      display: flex;
      align-items: center;
      margin-bottom: 24px;
      .col_title {
        width: 120px;
        text-align: right;
        margin-right: 8px;
      }
      .el-input,
      .el-textarea {
        width: 560px;
      }
      /deep/ .el-upload-dragger {
        width: 340px;
        height: 170px;
      }
      .upload-demo {
        margin-bottom: 0.8rem;
      }
    }
  }
}
</style>
